//----------------------------------------------------------- // Purpose: Read a maze file and search maze using recursion. // The program has very little error checking. // Author: John Gauch //----------------------------------------------------------- #include #include #include using namespace std; //----------------------------------------------------------- // Define the Maze class interface //----------------------------------------------------------- class Maze { public: // Constructors Maze(); ~Maze(); // Methods void ReadMaze(string name); void WriteMaze(string name); void PrintMaze(); bool SearchMaze(int r, int c); void GetMaze(int r, int c, char &value); void SetMaze(int r, int c, char value); void GetStart(int &r, int &c); void SetStart(int r, int c); void GetEnd(int &r, int &c); void SetEnd(int r, int c); private: int num_rows, num_cols; int start_row, start_col; int end_row, end_col; char maze[100][100]; }; //----------------------------------------------------------- // Constructor function //----------------------------------------------------------- Maze::Maze() { num_rows = 0; num_cols = 0; start_row = 0; start_col = 0; end_row = 0; end_col = 0; } //----------------------------------------------------------- // Destructor function //----------------------------------------------------------- Maze::~Maze() { } //----------------------------------------------------------- // Read maze from ascii file //----------------------------------------------------------- void Maze::ReadMaze(string name) { // Read maze.txt header ifstream din; din.open(name.c_str()); din >> num_rows >> num_cols; din >> start_row >> start_col; din >> end_row >> end_col; // Read maze.txt body string line; getline(din, line); for (int r = 0; r < num_rows; r++) { getline(din, line); for (int c = 0; c < num_cols; c++) if (c < int(line.length())) maze[r][c] = line[c]; } din.close(); } //----------------------------------------------------------- // Write maze to ascii file //----------------------------------------------------------- void Maze::WriteMaze(string name) { // Write maze.txt header ofstream dout; dout.open(name.c_str()); dout << num_rows << " " << num_cols << endl; dout << start_row << " " << start_col << endl; dout << end_row << " " << end_col << endl; // Write maze.txt body for (int r = 0; r < num_rows; r++) { for (int c = 0; c < num_cols; c++) dout << maze[r][c]; dout << endl; } dout.close(); } //----------------------------------------------------------- // Print maze to screen //----------------------------------------------------------- void Maze::PrintMaze() { // Print maze for (int r = 0; r < num_rows; r++) { for (int c = 0; c < num_cols; c++) cout << maze[r][c]; cout << endl; } } //----------------------------------------------------------- // Recursively search maze //----------------------------------------------------------- bool Maze::SearchMaze(int r, int c) { // Terminating conditions if ((r < 0) || (r >= num_rows) || (c < 0) || (c >= num_cols)) return false; else if (maze[r][c] != ' ') return false; // Recursive conditions maze[r][c] = '-'; if (((r == start_row) && (c == start_col)) || SearchMaze(r + 1, c) || SearchMaze(r - 1, c) || SearchMaze(r, c + 1) || SearchMaze(r, c - 1)) { cout << r << " " << c << endl; maze[r][c] = '.'; // PrintMaze(); return true; } else return false; } //----------------------------------------------------------- // Get start location //----------------------------------------------------------- void Maze::GetStart(int &r, int &c) { r = start_row; c = start_col; } //----------------------------------------------------------- // Set start location //----------------------------------------------------------- void Maze::SetStart(int r, int c) { start_row = r; start_col = c; } //----------------------------------------------------------- // Get end location //----------------------------------------------------------- void Maze::GetEnd(int &r, int &c) { r = end_row; c = end_col; } //----------------------------------------------------------- // Set end location //----------------------------------------------------------- void Maze::SetEnd(int r, int c) { end_row = r; end_col = c; } //----------------------------------------------------------- // Main program //----------------------------------------------------------- int main() { Maze m; int r, c; string filename; cout << "Enter maze filename: "; cin >> filename; m.ReadMaze(filename); m.PrintMaze(); m.GetStart(r, c); cout << "Start: " << r << " " << c << endl; m.GetEnd(r, c); cout << "End: " << r << " " << c << endl; m.SearchMaze(r, c); m.PrintMaze(); m.WriteMaze("maze.out"); }